package cn.user.controller;


import cn.entity.Do.Apply;
import cn.entity.Do.Bunk;
import cn.entity.Do.Room;
import cn.entity.Do.Suite;
import cn.entity.Status;
import cn.entity.User;
import cn.houseProperty.service.impl.BunkService;
import cn.houseProperty.service.impl.RoomService;
import cn.houseProperty.service.impl.SuiteService;
import cn.user.service.IuserService;
import cn.user.service.impl.ApplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/admin/user")
public class UserContoller {
    @Autowired
    private IuserService userService;

    @GetMapping("/login")
    public String login() {
        return "admin/login";
    }
    @GetMapping("/index")
    public String index(){
        return "admin/index";
    }
    @GetMapping("/test")
    public String Test(){return "admin/apply_form";}
    @ResponseBody
    @PostMapping("/login")
    public Status login(String username, String password, HttpSession session){
        Status status =new Status();
        User user = userService.findByUsernameAndPassword(username, password);
        if (user == null){
            status.setFlag(false);
            status.setMessage("用户名或密码错误...");
            return status;
        }else {
            System.out.println(user.getEntrytime());
            status.setFlag(true);
            status.setData(user);
            session.setAttribute("user",user);
            return status;
        }
    }
    @PostMapping("/info")
    public String info(User user){
        userService.updateByPrimaryKeySelective(user);
        return "admin/index";
    }

    @ResponseBody
    @GetMapping("/getUser")
    public User getUser(HttpSession session){
        User user = (User) session.getAttribute("user");
        user.setPassword("");
        return user;
    }
    @ResponseBody
    @PostMapping("/s")
    public Status send(Apply apply, HttpSession session){
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring.xml");
        BunkService bunkService= (BunkService) applicationContext.getBean("bunkService");
        RoomService roomService = (RoomService) applicationContext.getBean("roomService");
        SuiteService suiteService = (SuiteService) applicationContext.getBean("suiteService");
        ApplyService applyService = (ApplyService) applicationContext.getBean("applyService");
        Status status = new Status();
        User user = (User) session.getAttribute("user");
        apply.setUserid(user.getUid());
        apply.setStatus("0");
        if (apply.getRoom()!=null){
            if(apply.getBunk()!=null){
                Suite suite = suiteService.selectByPrimaryKey(apply.getSuite());
                if (suite.getStatus().equals(user.getSex())||suite.getStatus().equals("0")) {
                    status.setFlag(true);
                    status.setMessage("success");
                    Bunk bunk = bunkService.selectByPrimaryKey(apply.getBunk());
                    bunk.setStatus("1");
                    bunkService.updateByPrimaryKey(bunk);
                    applyService.insert(apply);
                    return status;
                //添加记录
                }else {
                    status.setFlag(false);
                    status.setMessage("您选择的套房有某些问题");
                    return status;
                }
            }else {
                List<Bunk> bunks = bunkService.selectByRid(apply.getRoom());
                if (bunks!=null) {
                    status.setFlag(false);
                    status.setMessage("您选择的房间有某些问题");
                    return status;
                }else {
                    status.setFlag(true);
                    status.setMessage("success");
                    Room room = roomService.selectByPrimaryKey(apply.getRoom());
                    room.setStatus('1');
                    roomService.updateByPrimaryKey(room);
                    applyService.insert(apply);
                    return status;
                    //添加记录
                }
            }
        }else {
            List<Bunk> bunks = bunkService.selectBySid(apply.getSuite());
            if (bunks != null) {
                status.setFlag(false);
                status.setMessage("您选择的房间有某些问题");
                return status;
            }else {
                status.setFlag(true);
                status.setMessage("success");
                applyService.insert(apply);
                return status;
                //添加记录
            }
        }
    }

    @ResponseBody
    @GetMapping("/selectAll")
    public Status selectAll(){
        Status status = new Status();
        List<User> users = userService.selectAll();
        if (users != null) {
            status.setFlag(true);
            status.setData(users);
            return status;
        }else{
            status.setFlag(false);
            status.setMessage("shibai");
            return status;
        }
    }
}
